//
//  LDStoreViewController.m
//  dmao-customer
//
//  Created by macbook on 2017/10/16.
//  Copyright © 2017年 秦慕乔. All rights reserved.
//

#import "LDStoreViewController.h"
#import "LineDownHomeTCell.h"
#import "LDPayViewController.h"

@interface LDStoreViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (strong, nonatomic) UIScrollView *sv;
@property (weak, nonatomic) IBOutlet UIView *TopView;
@property (weak, nonatomic) IBOutlet UIImageView *TopImg;
@property (weak, nonatomic) IBOutlet UIImageView *IconImg;
@property (weak, nonatomic) IBOutlet UILabel *StoreName;
@property (weak, nonatomic) IBOutlet UILabel *ScoreLabel;
@property (weak, nonatomic) IBOutlet UILabel *DateLabel;
@property (weak, nonatomic) IBOutlet UIImageView *CollectImg;
@property (weak, nonatomic) IBOutlet UILabel *AdressLabel;
@property (weak, nonatomic) IBOutlet UILabel *PhoneLabel;
@property (weak, nonatomic) IBOutlet UILabel *bottomLabel;
@property (strong, nonatomic) UITableView * tableView;
@property (strong, nonatomic) UIView *footerView;
@property (strong, nonatomic) NSMutableArray *ListArr;
@property (copy, nonatomic) NSString *collectStr;
@property (copy, nonatomic) NSString *agree;
@end

@implementation LDStoreViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    _ListArr = [[NSMutableArray alloc] init];
    [self downloadData];
    [self createTableView];
}

- (void)downloadData
{
    [self.view loadingOnAnyView];
    NSDictionary *dic = @{@"lineId":self.lineId,@"jwd":[Utils getJWD] ,@"userToken":[Utils getUserToken]};
    [THWManager PostHttp:API(LDStoreDetail) Parameters:dic Success:^(id responseObject) {
        [self.view removeAnyView];
        if (![TXUtilsString isBlankString:[Utils getData:responseObject][@"line"][@"shopImge"]]) {
            [_TopImg sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://%@", [Utils getData:responseObject][@"line"][@"shopImge"]]] placeholderImage:[UIImage imageNamed:@"NoStore"] options:SDWebImageRetryFailed];
        }else{
            [_TopImg setImage:[UIImage imageNamed:@"NoStore"]];
        }
        
        if (![TXUtilsString isBlankString:[Utils getData:responseObject][@"line"][@"shopImg"]]) {
            [_IconImg sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://%@", [Utils getData:responseObject][@"line"][@"shopImg"]]] placeholderImage:[UIImage imageNamed:@"NoStore"] options:SDWebImageRetryFailed];
        }else{
            [_IconImg setImage:[UIImage imageNamed:@"NoStore"]];
        }
        self.titleLabel.text = [Utils getData:responseObject][@"line"][@"shopName"];
        _StoreName.text = [NSString stringWithFormat:@"%@", [Utils getData:responseObject][@"line"][@"shopName"]];
        _ScoreLabel.text = [NSString stringWithFormat:@"信用分:%@分", [Utils getData:responseObject][@"line"][@"cs"]];
        _DateLabel.text = [NSString stringWithFormat:@"营业时间:%@-%@", [Utils getData:responseObject][@"line"][@"serviceStartTime"], [Utils getData:responseObject][@"line"][@"serviceEndTime"]];
        _AdressLabel.text = [NSString stringWithFormat:@"%@", [Utils getData:responseObject][@"line"][@"shopAddress"]];
        _PhoneLabel.text = [NSString stringWithFormat:@"%@", [Utils getData:responseObject][@"line"][@"shopTel"]];
        
        if (![Utils getData:responseObject][@"line"][@"favShop"]) {
            [_CollectImg setImage:[UIImage imageNamed:@"LDColloct"]];
        }else{
            _collectStr = [NSString stringWithFormat:@"%@", [Utils getData:responseObject][@"line"][@"favShop"]];
            if ([String([Utils getData:responseObject][@"line"][@"favShop"]) isEqualToString:@"0"]) {
                [_CollectImg setImage:[UIImage imageNamed:@"LDColloct"]];
                _agree = @"0";
            }else{
                [_CollectImg setImage:[UIImage imageNamed:@"LDCollocted"]];
                _agree = @"1";
            }
        }
        
        _ListArr = [[[Utils getData:responseObject] objectForKey:@"list"] objectForKey:@"Rows"];
        [_tableView reloadData];
        
    } andFalse:^(NSError *error) {
        [self.view removeAnyView];
    }];
}

- (void)createTableView
{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, _bottomLabel.bottom+(WIDTH*150/375-150+14), WIDTH, 222)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView registerNib:[UINib nibWithNibName:@"LineDownHomeTCell" bundle:nil] forCellReuseIdentifier:@"LineDownHomeTCell"];
    [_tableView setTableFooterView:_footerView];
    _tableView.showsVerticalScrollIndicator = NO;
    _tableView.scrollEnabled = NO;
    [_TopView addSubview:_tableView];
}

#pragma mark - UITableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _ListArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LineDownHomeTCell*cell = [tableView dequeueReusableCellWithIdentifier:@"LineDownHomeTCell" forIndexPath:indexPath];
    if (!cell) {
        cell = [[LineDownHomeTCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"LineDownHomeTCell"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell cellConfigureModel:_ListArr[indexPath.row]];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 111;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    LDStoreViewController *store = [[LDStoreViewController alloc] init];
    store.lineId = [_ListArr[indexPath.row] objectForKey:@"lineId"];
    [self.navigationController pushViewController:store animated:YES];
}

- (IBAction)CollectClick:(UIButton *)sender {
    if (!_collectStr) {
        NSDictionary *dic = @{@"id":self.lineId, @"type":@"3", @"userToken":[Utils getUserToken]};
        [self collcetClick:dic];
    }else{
        if ([String(_collectStr) isEqualToString:@"0"] && [_agree isEqualToString:@"0"]) {
            [_CollectImg setImage:[UIImage imageNamed:@"LDCollocted"]];
            NSDictionary *dic = @{@"id":self.lineId, @"type":@"3", @"userToken":[Utils getUserToken]};
            [self collcetClick:dic];
            _agree = @"1";
        }else{
            [_CollectImg setImage:[UIImage imageNamed:@"LDColloct"]];
            NSDictionary *dic = @{@"id":self.lineId, @"type":@"3", @"userToken":[Utils getUserToken]};
            [self CanceCollcetClick:dic];
            _agree = @"0";
        }
        
    }
}

- (void)collcetClick:(NSDictionary *)dic
{
    [self.view loadingOnAnyView];
    [THWManager PostHttp:API(LDAddFav) Parameters:dic Success:^(id responseObject) {
        [self.view removeAnyView];
        if ([String(responseObject[@"status_code"]) isEqualToString:@"1000"]) {
            [self.view Message:responseObject[@"msg"]];
        }else{
            [self.view Message:responseObject[@"msg"]];
        }
    } andFalse:^(NSError *error) {
        [self.view removeAnyView];
    }];
}

- (void)CanceCollcetClick:(NSDictionary *)dic
{
    [self.view loadingOnAnyView];
    [THWManager PostHttp:API(LDCanceFav) Parameters:dic Success:^(id responseObject) {
        [self.view removeAnyView];
        if ([String(responseObject[@"status_code"]) isEqualToString:@"1000"]) {
            [self.view Message:responseObject[@"msg"]];
        }else{
            [self.view Message:responseObject[@"msg"]];
        }
    } andFalse:^(NSError *error) {
        [self.view removeAnyView];
    }];
}

- (IBAction)PhoneClick:(UIButton *)sender {
    [Utils phone:_PhoneLabel.text controller:self];
}

- (IBAction)NearbyClick:(UIButton *)sender {
}
- (IBAction)PayClick:(UIButton *)sender {
    LDPayViewController *pay = [[LDPayViewController alloc] init];
    pay.lineId = self.lineId;
    [self.navigationController pushViewController:pay animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
